<?php

class Application_Model_AclRole extends Zend_Db_Table {
	protected $_name = 'acl_role';
	protected $_primary = 'id';

	public function getAllRoles( $_returnType = null ){
		try{
			$sql = $this->select()->setIntegrityCheck(false)
						->from(array('a' => $this->_name), array('id', 'name'))
						->joinLeft(array('b' => $this->_name), 'a.parent_id=b.id', array('parent'=>'name'));

			if ( $_returnType == 'query' ){
				return $sql;
			}

			$result = $this->getAdapter()->fetchAll($sql);
			if( $result )
				return $result;
			else
				return array();
		}
		catch(Zend_Exception $e){
			return array();
		}
	}
	
	/**
	 * Get role from acl_role by $id
	 * 
	 * @param mixed $id
	 * @return array
	 */
	public function getRole($id = null) {
		$idList = (!is_array($id)) ? array($id) : $id;
		$sql = $this->getPaginatorItems ();
		
		if ($id != null)
			foreach ($idList as $id)
				$sql->orWhere ('id = ' . $id);
		return $this->fetchAll($sql)->toArray();
	}
	
	/**
	 * Make the select for using with paginator
	 *
	 * @return (Zend_DB_Table_Select) $sql
	 */
	public function getPaginatorItems($where = null, $order = array('field' => null, 'type' => 'ASC')) {
		$sql = $this->select();
		# Customize your SQL here by adding join and where clauses
		
		$safeSql = clone($sql);
		if ($where != null) {
			$sql->where($where);
		}
		if(empty($order['type'])) {
			$order['type'] = 'ASC';
		}
		if($order && $order['field']) {
			$sql->order(array($order['field'].' '.$order['type']));
		}
		try {
			//Zend_Debug::dump($sql->__toString());
			$dbAdapter = Zend_Registry::get('dbAdapter');
			$dbAdapter->query($sql);
			return $sql;
		} catch(Zend_Exception $e) {
			//Zend_Debug::dump($safeSql->__toString());
			//Zend_Controller_Action_HelperBroker::getHelper('FlashMessenger')->addMessage('We are sorry but for the moment the filter is broken. You can still use this listing but without the use of the filter from. <a href="http://proiecte.dev/framework/newticket">(report this bug)</a>');
			return $safeSql;
		}
		return $sql;
	}

	public function getAclRecurringRoles( $_start_id ){
		$arrResult = array();

		$sql = $this->select()->setIntegrityCheck(false)
						->from(array('a' => $this->_name), array('id', 'name'))
						->joinLeft(array('b' => $this->_name), 'a.parent_id=b.id', array('parent'=>'name'))
						->where('a.parent_id = ?', $_start_id);
		$result = $this->getAdapter()->fetchAll($sql);

		if ( count($result) ){
			foreach ( $result as $row ){
				$arrResult[] = $row;
				$arrResult = array_merge($arrResult, $this->getAclRecurringRoles($row['id']));
			}
		}
		return $arrResult;
	}

	public function getRecurringRoles( $_start_id, $_exclude_id = null ){
		$arrResult = array();

		$sql = $this->select()->setIntegrityCheck(false)
					->from($this->_name, array('id', 'name'))
					->where('parent_id = ?', $_start_id);
		$result = $this->getAdapter()->fetchAll($sql);

		if ( count($result) ){
			foreach ( $result as $row ){
				if ( $row['id'] != $_exclude_id ){
					$arrResult[$row['id']] = $row['name'];
					$arrResult = $arrResult + $this->getRecurringRoles($row['id'], $_exclude_id);
				}
			}
		}
		return $arrResult;
	}

	public function getRootId(){
		try{
			$sql = $this->select()->setIntegrityCheck(false)
						->from($this->_name, array('id'))
						->where('parent_id = 0');

			$result = $this->getAdapter()->fetchRow($sql);
			if( $result )
				return $result['id'];
			else
				return 0;
		}
		catch(Zend_Exception $e){
			return 0;
		}
	}

	public function getRoleById( $_id ){
		try{
			$sql = $this->select()->setIntegrityCheck(false)
						->from(array('a' => $this->_name), array('id', 'name'))
						->joinLeft(array('b' => $this->_name), 'a.parent_id=b.id', array('parent'=>'name'))
						->where('a.id = ?', $_id);

			$result = $this->getAdapter()->fetchRow($sql);
			if( $result )
				return $result;
			else
				return 0;
		}
		catch(Zend_Exception $e){
			return 0;
		}
	}

	public function getRoleIdByName($_role_name = null) {

		$sql = $this->select()->setIntegrityCheck(false)
					->from('acl_role', array('id'))
					->where('name =  ?' , $_role_name);
		$result = $this->fetchRow($sql);

		return $result->id;
	}

}